package com.distortiongames.level27.scenes 
{
	import com.distortiongames.level27.components.render.TileRenderer;
	import com.distortiongames.level27.GCam;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.engine.TextBlock;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author Jefferson Wagner
	 */
	public class ViewScene extends MapScene 
	{
		private var firstTick:Boolean = true;
		private var camera:GCam;
		private var positionX:Number;
		private var positionY:Number;
		
		override public function initialize():void 
		{
			super.initialize();
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			var string:String = "";
			for (var key:Object in generator.lastPhrase.properties) {
				string += key + ": " + generator.lastPhrase.properties[key]+"\n";
			}
			var text:TextField = new TextField();
			text.width = 400;
			text.text = generator.lastPhrase.toString() +"\n"+string;
			text.textColor = 0xFFFFFF;
			stage.addChild(text);
		}
		
		public override function onTick():void
		{
			super.onTick();
			if (firstTick)
			{
				positionX = generator.start.x * TileRenderer.WIDTH;
				positionY = generator.start.y * TileRenderer.HEIGHT;
				var realBounds:Rectangle = canvas.getBounds(canvas);
				var fakeBounds:Rectangle = new Rectangle( -realBounds.width*2, -realBounds.height*2, realBounds.width * 3, realBounds.height * 3);
				camera = new GCam(canvas, realBounds);
				camera.to(positionX, positionY, 2, 0);
				firstTick = false;
			}
			
			var panLeft:Boolean = keyboardManager.isKeyDown(Keyboard.LEFT);
			var panRight:Boolean = keyboardManager.isKeyDown(Keyboard.RIGHT);
			var panDown:Boolean = keyboardManager.isKeyDown(Keyboard.DOWN);
			var panUp:Boolean = keyboardManager.isKeyDown(Keyboard.UP);
			
			if (panLeft || panRight || panDown || panUp) {
				var panX:Number = (panLeft) ? -10 : 0 + (panRight) ? 10 : 0;
				var panY:Number = (panUp) ? -10 : 0 + (panDown) ? 10 : 0;
				camera.to(camera.lastX + panX, camera.lastY + panY, camera.lastZoomAmount, .2);
			}
		}
		
		private function mouseWheel(e:MouseEvent):void {
			var sign:int = (e.delta < 0) ? -1 : 1;
			if (camera.lastZoomAmount + sign * .2 >= .8 && camera.lastZoomAmount + sign * .2 < 4.2)
			camera.to(camera.lastX, camera.lastY, camera.lastZoomAmount + sign * .2, .2);
		}
		
		override public function destroy():void 
		{
			super.destroy();
		}
		
	}

}